package com.ztgf.order.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.order.common.config.YmlConfig;
import com.ztgf.order.mapper.OrderTimeRecordMapper;
import com.ztgf.order.models.bo.OrderTimeRecordSaveBO;
import com.ztgf.order.models.dto.OrderTimeRecordDTO;
import com.ztgf.order.models.service.IOrderTimeRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;

/**
 * 订单节点时间记录表控制器
 *
 * @author chenyuwang
 * @Date 2021-03-09 14:44:11
 */
@Slf4j
@DubboService
public class OrderTimeRecordServiceImpl extends ServiceImpl<OrderTimeRecordMapper, OrderTimeRecordDTO> implements IOrderTimeRecordService {

    @Resource
    private YmlConfig ymlConfig;

    @Override
    public void add(OrderTimeRecordSaveBO saveBO) {
        try {
            // 先判断是否存在
            Wrapper<OrderTimeRecordDTO> wrapper = new EntityWrapper<>();
            wrapper.eq("order_id", saveBO.getOrderId());
            wrapper.eq("order_state", saveBO.getOrderState());
            OrderTimeRecordDTO selectOne = this.selectOne(wrapper);
            if (selectOne != null) {
                return;
            }
            OrderTimeRecordDTO dto = new OrderTimeRecordDTO();
            BeanUtils.copyProperties(saveBO, dto);
            Long id = ymlConfig.getPkId();
            dto.setId(id);
            this.insert(dto);
        } catch (Exception e) {
            log.error("添加订单节点时间记录错误信息:{}", e.getMessage());
            e.printStackTrace();
        }

    }

    @Override
    public boolean insert(OrderTimeRecordSaveBO saveBO) {
        OrderTimeRecordDTO dto = new OrderTimeRecordDTO();
        BeanUtils.copyProperties(saveBO, dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }
}
